package com.colorfruit.admin.business.service.impl;

import com.colorfruit.admin.business.dao.*;
import com.colorfruit.admin.business.service.DistributorService;
import com.colorfruit.admin.pojo.constant.AliConstant;
import com.colorfruit.admin.pojo.entity.FoundationPickGoodsAddress;
import com.colorfruit.admin.pojo.entity.FoundationUserReceiveAddress;
import com.colorfruit.admin.pojo.entity.GlobalProcessionControl;
import com.colorfruit.admin.pojo.entity.PayBusinessOrder;
import com.colorfruit.admin.pojo.enums.AddressTypeEnum;
import com.colorfruit.admin.pojo.enums.DistributeStatusEnum;
import com.colorfruit.admin.pojo.enums.ErrorCodeEnum;
import com.colorfruit.admin.pojo.enums.GlobalStatusEnum;
import com.colorfruit.admin.pojo.vo.distributer.PayBusinessOrderDistributeVo;
import com.colorfruit.admin.pojo.vo.payorder.PayOrderItemVo;
import com.colorfruit.framework.model.pojo.BaseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class DistributorServiceImpl implements DistributorService {

    @Autowired
    PayBusinessOrderMapper payBusinessOrderMapper;

    @Autowired
    PayBusinessOrderItemMapper payBusinessOrderItemMapper;

    @Autowired
    GlobalProcessionControlMapper globalProcessionControlMapper;

    @Autowired
    FoundationPickGoodsAddressMapper foundationPickGoodsAddressMapper;

    @Autowired
    FoundationUserReceiveAddressMapper foundationUserReceiveAddressMapper;

    @Override
    public BaseResult selectBusinessOrderList(Long pickGoodsAddressId,
                                              String keyWord,
                                              Byte distributeStatus,
                                              int page, int size) {
        log.info("[service] [IN] selectBusinessOrderList param : pickGoodsAddressId = {}, keyWord = {}, distributeStatus = {}, page = {}, size = {}"
                , pickGoodsAddressId, keyWord , distributeStatus, page, size);
        GlobalProcessionControl recentProcession = globalProcessionControlMapper.getRecentProcession();
        List<PayBusinessOrderDistributeVo> payBusinessOrderDistributeVos = new ArrayList<>();
        PageHelper.startPage(page, size);
        List<PayBusinessOrder> payBusinessOrders = payBusinessOrderMapper.selectOrderListByCondition(recentProcession.getId(),
                pickGoodsAddressId,
                keyWord,
                distributeStatus);
        PageInfo<PayBusinessOrder> payBusinessOrderPageInfo = new PageInfo<>(payBusinessOrders);
        for (PayBusinessOrder payBusinessOrder : payBusinessOrders){
            PayBusinessOrderDistributeVo payBusinessOrderDistributeVo = new PayBusinessOrderDistributeVo();
            payBusinessOrderDistributeVo.setBusinessNo(payBusinessOrder.getBusinessOrderNo());
            payBusinessOrderDistributeVo.setPhone(payBusinessOrder.getPhone());
            payBusinessOrderDistributeVo.setUserName(payBusinessOrder.getUserName());
            payBusinessOrderDistributeVo.setDistributeStatus(payBusinessOrder.getDistributeStatus());
            Byte addressType = payBusinessOrder.getAddressType();
            if (addressType == AddressTypeEnum.PERSONAL_ADDRESS.getCode()){
                FoundationUserReceiveAddress foundationUserReceiveAddress = foundationUserReceiveAddressMapper.selectByPrimaryKey(payBusinessOrder.getReceiveAddressId());
                payBusinessOrderDistributeVo.setAddressDetail(foundationUserReceiveAddress.getAddressDescription());
            }else if (addressType == AddressTypeEnum.PICKING_ADDRESS.getCode()){
                FoundationPickGoodsAddress foundationPickGoodsAddress = foundationPickGoodsAddressMapper.selectByPrimaryKey(payBusinessOrder.getReceiveAddressId());
                payBusinessOrderDistributeVo.setAddressDetail(foundationPickGoodsAddress.getAddressDescription());
            }
            List<PayOrderItemVo> payOrderItemVos = payBusinessOrderItemMapper.selectOrderItemList(payBusinessOrder.getBusinessOrderNo());
            payBusinessOrderDistributeVo.setItemList(payOrderItemVos);
            payBusinessOrderDistributeVos.add(payBusinessOrderDistributeVo);
        }
        PageInfo<PayBusinessOrderDistributeVo> payBusinessOrderDistributeVoPageInfo = new PageInfo<>(payBusinessOrderDistributeVos);
        payBusinessOrderDistributeVoPageInfo.setSize(payBusinessOrderPageInfo.getSize());
        log.info("[service] [OUT] selectBusinessOrderList success ");
        return new BaseResult(payBusinessOrderDistributeVoPageInfo);
    }

    @Override
    public BaseResult addLogicPicture(String businessNo, String logicPicture) {
        log.info("[service] [IN] addLogicPicture param : businessNo = {}", businessNo);
        String pictureUrl = AliConstant.DOMAIN + AliConstant.IMAGE + logicPicture;
        payBusinessOrderMapper.addLogicPicture(businessNo, pictureUrl);
        log.info("[service] [OUT] addLogicPicture success ");
        return new BaseResult(ErrorCodeEnum.SUCCESS);
    }

    @Override
    public BaseResult updateDistributing(String  businessNo) {
        log.info("[service] [IN] updateDistributeStatus param : businessNo = {}", businessNo);
        GlobalProcessionControl recentProcession = globalProcessionControlMapper.getRecentProcession();
        if (recentProcession.getRecordStatus() != GlobalStatusEnum.TRANSPORT.getCode()){
            return new BaseResult(ErrorCodeEnum.DATA_ERROR.getCode(), "非运输阶段,配送");
        }
        payBusinessOrderMapper.updateDistributeStatus(businessNo, DistributeStatusEnum.DISTRIBUTING.getCode());
        log.info("[service] [OUT] updateDistributeStatus success ");
        return new BaseResult(ErrorCodeEnum.SUCCESS);
    }
}
